package com.cb.kfc.dao;

import com.cb.kfc.domain.Friend;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface IFriendDao {
    /**
     * 直接从friend表里查是查不出的，要先从中间表查
     * @param ordersId
     * @return
     * @throws Exception
     */
    @Select("select * from friend where id  in (select friendId from order_friend where orderId=#{ordersId})")
     public List<Friend> findByOrdersId(String ordersId) throws Exception;

    /**
     * 直接从friend表里查是查不出的，要先从中间表查
     * @param memberid
     * @return
     * @throws Exception
     */
    @Select("select * from friend where id  in (select friendId from MEMBER_FRIEND where MEMBERId=#{memberid})")
    public List<Friend> findByMemberId(String memberid) throws Exception;


    //查询本会员亲友的个人信息
    @Select("select * from friend")
    public List<Friend> findAll() throws Exception;

    @Insert("insert into friend(name,phoneNum) values(#{name},#{phoneNum})")
    void save(Friend friend);

    //删除单个，或者批量删除
    @Delete("<script>delete from ORDER_FRIEND where friendid in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer deleteOF(String[] ids);


    //删除单个，或者批量删除
    @Delete("<script>delete from MEMBER_FRIEND where friendid in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer deleteMF(String[] ids);


    //删除单个，或者批量删除
    @Delete("<script>delete from friend where id in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer deleteF(String[] ids);

    //查找单个
    @Select("select * from friend where id = #{id}")
    Friend findOne(String id);

    //更新
    @Update("update friend set  name=#{name},phoneNum=#{phoneNum} where id =#{id} ")
    int update(Friend friend);


}
